package net.qhzw.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.admin.entity.PersonnelLearninginfo;
import net.qhzw.admin.mapper.PersonnelLearninginfoMapper;
import net.qhzw.admin.param.PersonnelLearninginfoParam;
import net.qhzw.admin.pojo.PersonnelLearninginfoVo;
import net.qhzw.admin.pojo.PersonnelLearninginfoVo2;
import net.qhzw.admin.pojo.PersonnelRewardVo;
import net.qhzw.admin.query.PersonnelLearninginfoQuery;
import net.qhzw.admin.service.PersonnelLearninginfoService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PersonnelLearninginfoServiceImpl implements PersonnelLearninginfoService {


    @Autowired
    private PersonnelLearninginfoMapper personnelLearninginfoMapper;

    @Value("${config.imgage.view}")
    private String imageView;

    @Override
    public JsonResult getPersonnelLearninginfo(String id) {
        PersonnelLearninginfoVo2 personnelLearninginfoVo2 = personnelLearninginfoMapper.selectPersonnelLearninginfo(id);
        Date[] times = new Date[2];
        if (personnelLearninginfoVo2.getStartTime() != null) {
            times[0] = personnelLearninginfoVo2.getStartTime();
        }
        if (personnelLearninginfoVo2.getEndTime() != null) {
            times[1] = personnelLearninginfoVo2.getEndTime();
        }
        if (times != null && times.length > 0) {
            personnelLearninginfoVo2.setLearningTime(times);
        }
        return JsonResult.success(personnelLearninginfoVo2);
    }

    @Override
    public JsonResult getPersonnelLearninginfoList(PersonnelLearninginfoQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        if (query.getLearningTime() != null && query.getLearningTime().length > 0) {
            query.setStartTime(query.getLearningTime()[0]);
            query.setEndTime(query.getLearningTime()[1]);
        }
        List<PersonnelLearninginfoVo> personnelLearninginfoList = personnelLearninginfoMapper.selectList(query);
        for (PersonnelLearninginfoVo personnelRewardVo : personnelLearninginfoList) {
            if (personnelRewardVo.getPhoto() != null && !StringUtils.isEmpty(personnelRewardVo.getPhoto())) {
                personnelRewardVo.setPhoto(imageView + personnelRewardVo.getPhoto());
            }
        }
        PageInfo<PersonnelLearninginfoVo> pageInfo = new PageInfo<>(personnelLearninginfoList);
        result.put("total", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return JsonResult.success(result);
    }

    @Override
    public JsonResult deletePersonnelLearninginfo(String id) {
        int row = personnelLearninginfoMapper.deleteByPrimaryKey(id);
        return JsonResult.result(row, "删除失败");
    }

    @Override
    public JsonResult update(PersonnelLearninginfoParam param) {
        if (param.getId() == null || StringUtils.isEmpty(param.getId())) {
            if (param.getPersonnelId() == null || StringUtils.isEmpty(param.getPersonnelId())) {
                return JsonResult.error("人员id不能能为空");
            }
            //新增
            PersonnelLearninginfo personnelLearninginfo = new PersonnelLearninginfo();
            BeanUtils.copyProperties(param, personnelLearninginfo);
            personnelLearninginfo.setId(UUIDUtil.getUUid());
            personnelLearninginfo.setCreateTime(new Date());
            if (param.getLearningTime() != null && param.getLearningTime().length > 1) {
                personnelLearninginfo.setStartTime(param.getLearningTime()[0]);
                personnelLearninginfo.setEndTime(param.getLearningTime()[1]);
            }
            int row = personnelLearninginfoMapper.insert(personnelLearninginfo);
            return JsonResult.result(row, "新增失败");
        }
        PersonnelLearninginfo personnelLearninginfo = new PersonnelLearninginfo();
        BeanUtils.copyProperties(param, personnelLearninginfo);
        if (param.getLearningTime() != null && param.getLearningTime().length > 1) {
            personnelLearninginfo.setStartTime(param.getLearningTime()[0]);
            personnelLearninginfo.setEndTime(param.getLearningTime()[1]);
        }
        int row = personnelLearninginfoMapper.updateByPrimaryKeySelective(personnelLearninginfo);
        return JsonResult.result(row, "修改失败");
    }
}
